<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hadd">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 要放在el-card里面 -->
        <el-tree
          :data="list"
          default-expand-all
        >
          <!-- 数据要在el组件里面用 ,用slot作用域插槽 -->
          <template #default="{ data }">

            <!-- 用一个行列布局 -->
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length===0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row></template>
        </el-tree>
      </el-card>

      <!-- 弹窗 -->
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :title="isEdit?'编辑':'添加'"
        :visible.sync="isshowDialog"
      ><isDialog
        v-if="isshowDialog"
        :id="curId"
        ref="deptDialog"
        :origin-list="originList"
        :is-edit="isEdit"
        @success="hSuccess"
        @close="hClose"
      /></el-dialog>
    </div>
  </div>
</template>
<script>
//
import { getDepartments, delDepartment } from '@/api/departments'

//
import { tranListTreeData } from '@/utils/index'

//
import isDialog from './deptDialog.vue'
export default {
  components: { isDialog },
  data() {
    return {
      //
      originList: [],
      //
      isshowDialog: false,
      // 依赖一份树形数据
      list: [
      ],
      curId: '',
      //
      isEdit: false // 是否处于编辑状态
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 收到子组件中的close事件
    hClose() {
      this.isshowDialog = false
    },
    // 用户点击删除
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res)
        // 1. 提示
        this.$message({ type: 'success', message: '删除成功!' })
        // 2. 更新数据
        this.loadDepartments()
      } catch (err) {
        console.log(err)
        this.$message({ type: 'error', message: '删除失败' })
      }
    },
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 做删除
        this.doDel(id)
      }).catch(() => {

      })
    },
    //
    hEdit(id) {
      // 1. 弹层出来
      this.isshowDialog = true
      // 2. 表单中要显示详情数据
      // 把id传给子组件，在子组件中去获取详情
      // this.curId = id
      // 3. 传递一个标识给子组件，告诉它我们现在是编辑状态
      this.isEdit = true

      // 获取子组件的引用
      this.$nextTick(() => {
        console.log('获取子组件的引用', this.$refs.deptDialog)
        console.log('当前的curId', id)
        console.log('获取到子组件中的id--从父传入的', this.$refs.deptDialog.id)
        // 调用子组件的方法
        this.$refs.deptDialog.loadDepartmentDetailById()
      })
    },
    //
    hSuccess() {
      // 关闭弹层，再次更新数据
      this.isshowDialog = false

      this.loadDepartments()
    },
    // 显示弹窗
    hAdd(id) {
      // console.log(1)
      this.isshowDialog = true // 显示
      // 3. 传递一个标识给子组件，告诉它我们现在是编辑状态
      this.isEdit = false
    },
    async loadDepartments() {
      try {
        const res = await getDepartments()
        // console.log(res, 'RES')
        this.originList = res.data.depts.map((item) => {
          return { id: item.id, name: item.name, code: item.code, pid: item.pid }
        })
        // 注意数组的shift方法,返回的是删除的数组的元素
        // 但是原来的数组,剩下的是,新的数组
        res.data.depts.shift()
        // tranListTreeData(res.data.depts)
        // console.log('treelist', tranListTreeData(res.data.depts))
        //
        this.list = tranListTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
